<template>
  <div>
    <el-form ref="form" :model="form" inline>
      <el-form-item label="校区" style="font-weight:bold;">
        <el-select v-model="form.campusId" placeholder="请选择校区" style="width:150px;" clearable>
          <el-option v-for="item in CampusList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼宇" style="font-weight:bold;">
        <el-select v-model="form.buildId" placeholder="请选择楼宇" style="width:150px">
          <el-option v-for="item in buildName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="房间号" style="font-weight:bold;">
        <el-input v-model="form.roomNum" style="width:150px" placeholder="请输入房间号"></el-input>
      </el-form-item>
      <el-button size="small" class="search_button" type="primary" icon="el-icon-search" @click="getForm">查询</el-button>
      <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
    </el-form>
    <el-button size="small" type="success" plain icon="el-icon-download" @click="exportList">导出评分信息</el-button>
    <el-divider></el-divider>


    <el-table :data="tableData" border size="medium">
      <el-table-column prop="campusName" label="校区" align="center">
      </el-table-column>
      <el-table-column prop="buildName" label="楼宇" align="center">
      </el-table-column>
      <el-table-column prop="roomNum" label="房间号" align="center">
      </el-table-column>
      <el-table-column prop="score" label="评分" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="评分时间" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="lookDetail(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>

    <el-dialog title="评分详情" :visible.sync="showDetail" width="50%">
      <el-descriptions style="margin-top: 10px;" :title="item.name" :column="2" size="medium" border
        v-for="item in listDetail" :key="item.id">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-edit-outline"></i>
            评分
          </template>
          {{ item.score }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            评分人
          </template>
          {{ item.operator }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-camera"></i>
            照片
          </template>
          <el-button icon="el-icon-view" plain type="text" @click="showImg(item.imgUrl)">查看</el-button>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-order"></i>
            卫生情况
          </template>
          {{ item.infomation }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <el-dialog title="查看照片" :visible.sync="showPic" width="50%">
      <el-image style="width:200px" :src="picUrl" :preview-src-list="srcList" rel="no-referrer"></el-image>
    </el-dialog>
  </div>
</template>

<script>

import { getCampusName, getBuildingName } from '_api/DormManage/Building/index.js'
import { getForm, getDetail, exportList } from '_api/DormManage/SanitaryExamination/index.js'
export default {
  data() {
    return {
      showPic: false,
      picUrl: "",
      srcList: [],
      showDetail: false,
      form: {
        current: 0,
        pageSize: 10,
        buildId: "",
        campusId: "",
        roonNum: ""
      },
      tableData: [],
      total: 0,
      CampusList: [],
      buildName: [],
      listDetail: [],
      timeNow: ""
    }
  },
  watch: {
    form: {
      // 执行方法
      handler(oldValue, newValue) {
        getBuildingName({ campusId: newValue.campusId, type: 0 }).then(res => {
          if (res.status == 200) {
            this.buildName = res.data.data
          }
        })
      },
      deep: true, // 深度监听
      // immediate: true  // 第一次改变就执行
    }
  },
  created() {
    this.getCampusName()
    this.getForm()
  },
  methods: {
    showImg(e) {
      this.srcList = []
      this.showPic = true
      this.picUrl = e
      this.srcList.push(e)
    },
    lookDetail(e) {
      getDetail({ special: e.special }).then(res => {
        if (res.data.success == true) {
          this.listDetail = res.data.data
          this.showDetail = true
        } else {
          this.$message(res.data.message)
        }
      })
    },
    //重置
    reset() {
      this.form = {
        current: 0,
        pageSize: 10,
        buildId: "",
        campusId: "",
        roonNum: ""
      }
      this.getForm()
    },
    //当前页
    handleCurrentChange(e) {
      this.form.current = (e - 1) * 10
      this.getForm()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getForm()
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
      this.timeNow = _this.gettime
    },
    exportList() {
      exportList(this.form).then(res => {
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '宿舍卫生检查报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    //查询校区名称
    getCampusName() {
      getCampusName().then(res => {
        console.log(res)
        if (res.status == 200) {
          this.CampusList = res.data.data
          this.getBuildingName()
        }
      })
    },
    //获取楼宇名称
    getBuildingName() {
      getBuildingName({ type: 0 }).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.buildName = res.data.data

        }
      })
    },

    //查询列表信息
    getForm() {
      getForm(this.form).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>